package com.game.state;

import javax.microedition.khronos.opengles.GL10;

import media.sound.GameSound;
import media.sound.SoundBackground;

import com.game.GamePlay;
import com.game.R;

import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;

import system.Device;
import system.GameOption;
import system.Graphics;

public class BackCountry extends State {
	
	private GL10 gl;
	private int m_fSrcWidth;
	private int m_fSrcHeight;
	private Texture m_texture;
	
	public void Destroy() {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPopMatrix();
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPopMatrix();
	}

	
	public void Init() {
		gl = Graphics.GetGraphics();
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glPushMatrix();
		gl.glLoadIdentity();
		float w = m_fSrcWidth/2;
		float h = m_fSrcHeight/2;
		gl.glOrthof(-w, w, -h, h, -1.0f, 1.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		
		m_texture = new Texture();
		
		LoadCongratulations();
		LoadInstructor();
		LoadProgrammer();
		LoadAbout();
	}

	
	private int m_iCurScreen = 0;
	public void Render() {
		
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		switch(m_iCurScreen)
		{
		case 0:
			DrawCongratulations();
			break;
		case 1:
			DrawInstructor();
			break;
		case 2:
			DrawProgrammer();
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			DawAbout();
			break;
		}
	}
	
	public void Update(GamePlay gamePlay) {
		switch(m_iCurScreen)
		{
		case 0:
			UpdateCongratulations();
			break;
		case 1:
			UpdateInstructor();
			break;
		case 2:
			UpdateProgrammer();
			break;
		case 3:
			++m_iCurScreen;
			break;
		case 4:
			++m_iCurScreen;
			m_lastUpdateAlpha = System.nanoTime();
			break;
		case 5:
			UpdateAbout();
			break;
		}
	}
	
	//////////////////////////////////////////////
	// CONGRATULATIONS
	private int m_iCongratulationsID;
	private TexSquare m_congratulationsSquare;
	private int m_iCongratulationsW = 280;
	private int m_iCongratulationsH = 64;
	
	private long m_iCongraLastTime;
	private static final int CONGRATULATION_SPLASH_TIME = 500;
	private boolean m_isCongraDisplay = false;
	private int m_iCongraSplashCount = 0;
	
	public void LoadCongratulations()
	{
		m_iCongratulationsID = m_texture.initTexture(R.drawable.backcountry_congratulations);
		m_congratulationsSquare = new TexSquare(m_iCongratulationsW, m_iCongratulationsH);
		m_iCongraLastTime = System.nanoTime();
	}
	
	public void DrawCongratulations()
	{
		if(m_isCongraDisplay)
		{
			m_texture.loadTexture(m_iCongratulationsID);
			m_congratulationsSquare.Render();
		}	
	}
	
	public void UpdateCongratulations()
	{
		if((System.nanoTime() - m_iCongraLastTime)/1000000 >= CONGRATULATION_SPLASH_TIME)
		{
			++m_iCongraSplashCount;
			if(m_iCongraSplashCount == 10)
			{
				++m_iCurScreen;
				if(GameOption.isMusicOn)
				{
					GameSound.SetBgSound(new SoundBackground(Device.GetContext()));
					GameSound.GetBgSound().PlayBackCountry();
				}
			}
			m_isCongraDisplay = !m_isCongraDisplay;
			m_iCongraLastTime = System.nanoTime();
		}
	}
	
	//////////////////////////////////////////////
	// INSTRUCTOR
	private int m_iInstructorID;
	private TexSquare m_instructorSquare;
	private int m_iInstructorW = 256;
	private int m_iInstructorH = 128;
	private float m_iInstructorTrans = ((float)480 + m_iInstructorW)/2 + 10;
	private float m_iTrans = m_iInstructorTrans;
	private boolean m_isInstructorStop = false;
	private static final int INSTRUCTOR_STOP_TIME = 1000;
	private long m_iInstructorLastStop;
	private boolean m_isStartStop = false;
	
	public void LoadInstructor()
	{
		m_iInstructorID = m_texture.initTexture(R.drawable.backcountry_instructor);
		m_instructorSquare = new TexSquare(m_iInstructorW, m_iInstructorH);
		
	}
	public void DrawInstructor()
	{
		gl.glPushMatrix();
		gl.glTranslatef(m_iTrans, 0, 0);
		
		m_texture.loadTexture(m_iInstructorID);
		m_instructorSquare.Render();
		
		gl.glPopMatrix();
	}
	
	public void UpdateInstructor()
	{
		if(m_iTrans >= -m_iInstructorTrans)
		{
			if(!m_isInstructorStop)
			{
				m_iTrans -= 10;
			}
			else
			{
				if((System.nanoTime() - m_iInstructorLastStop)/1000000 >= INSTRUCTOR_STOP_TIME)
					m_isInstructorStop = false;
			}
			
			if(!m_isStartStop && m_iTrans <= 0)
			{
				m_isInstructorStop = true;
				m_isStartStop = true;
				m_iInstructorLastStop = System.nanoTime();
			}
		}
		else
		{
			++m_iCurScreen;
		}
	}
	
	///////////////////////////////////////////
	// PROGRAMMER
	private int m_iProgrammerID;
	private TexSquare m_programmerSquare;
	private int m_iProgrammerW = 256;
	private int m_iProgrammerH = 128;
	private float m_iProgrammerTrans = ((float)480 + m_iProgrammerW)/2 + 10;
	private float m_iProgrammerCurTrans = m_iProgrammerTrans;
	private boolean m_isProgrammerStop = false;
	private static final int PROGRAMMER_STOP_TIME = 1000;
	private long m_iProgrammerLastStop;
	private boolean m_isProgrammerStartStop = false;
	
	public void LoadProgrammer()
	{
		m_iProgrammerID = m_texture.initTexture(R.drawable.backcountry_programmer);
		m_programmerSquare = new TexSquare(m_iProgrammerW, m_iProgrammerH);
	}
	
	public void DrawProgrammer()
	{
		gl.glPushMatrix();
		gl.glTranslatef(m_iProgrammerCurTrans, 0, 0);
		
		m_texture.loadTexture(m_iProgrammerID);
		m_programmerSquare.Render();
		
		gl.glPopMatrix();
	}
	
	public void UpdateProgrammer()
	{
		if(m_iProgrammerCurTrans >= -m_iProgrammerTrans)
		{
			if(!m_isProgrammerStop)
			{
				m_iProgrammerCurTrans -= 10;
			}
			else
			{
				if((System.nanoTime() - m_iProgrammerLastStop)/1000000 >= PROGRAMMER_STOP_TIME)
					m_isProgrammerStop = false;
			}
			
			if(!m_isProgrammerStartStop && m_iProgrammerCurTrans <= 0)
			{
				m_isProgrammerStop = true;
				m_isProgrammerStartStop = true;
				m_iProgrammerLastStop = System.nanoTime();
			}
		}
		else
		{
			++m_iCurScreen;
		}
	}
	
	////////////////////////////////////////////
	// ABOUT
	private int m_iTitleID;
	private int m_iAboutID;
	private int m_iPressKeyID;
	private TexSquare m_titleSquare;
	private TexSquare m_aboutSquare;
	private TexSquare m_pressKeySquare;
	private int m_iTitleWidth = 350;
	private int m_iTitleHeight = 60;
	private int m_iAboutWidth = 256;
	private int m_iAboutHeight = 256;
	private int m_iPressKeyWidth = 256;
	private int m_iPressKeyHeight = 64;
	
	private float m_aboutAlpha = 0;
	
	private boolean m_isUpdateAlpha = true;
	private long m_lastUpdateAlpha;
	private long UPDATE_ALPHA_TIME = 100;
	
	private int PRESS_KEY_TIME = 400;
	private boolean m_isDisplayPressKey = true;
	private long m_lastPressKeyTime = System.nanoTime();
	
	public void LoadAbout()
	{
		m_titleSquare = new TexSquare(m_iTitleWidth, m_iTitleHeight);
		m_aboutSquare = new TexSquare(m_iAboutWidth, m_iAboutHeight);
		m_pressKeySquare = new TexSquare(m_iPressKeyWidth, m_iPressKeyHeight);
		
		m_iTitleID = m_texture.initTexture(R.drawable.gamename);
		m_iAboutID = m_texture.initTexture(R.drawable.backcountry_about);
		m_iPressKeyID = m_texture.initTexture(R.drawable.backcountry_pressanykey);
	}
	
	public void DawAbout()
	{
		gl.glColor4f(1.0f, 1.0f, 1.0f, m_aboutAlpha);
		
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		
		gl.glPushMatrix();
		gl.glTranslatef(0, (m_iAboutHeight >> 1), 0);
		m_texture.loadTexture(m_iTitleID);
		m_titleSquare.Render();
		gl.glPopMatrix();
		
		m_texture.loadTexture(m_iAboutID);
		m_aboutSquare.Render();
		
		if(m_isDisplayPressKey)
		{
			gl.glTranslatef(0, -(m_iAboutHeight >> 1), 0);
			m_texture.loadTexture(m_iPressKeyID);
			m_pressKeySquare.Render();
		}
		
		gl.glPopMatrix();
	}
	
	public void UpdateAbout()
	{
		if(m_isUpdateAlpha && (System.nanoTime() - m_lastUpdateAlpha)/1000000 >= UPDATE_ALPHA_TIME)
		{
			m_aboutAlpha += 0.1f;
			 m_lastUpdateAlpha = System.nanoTime();
			if(m_aboutAlpha >= 1.0f) m_isUpdateAlpha = false;
		}
		
		if((System.nanoTime() - m_lastPressKeyTime)/1000000 > PRESS_KEY_TIME)
		{
			m_lastPressKeyTime = System.nanoTime();
			m_isDisplayPressKey = !m_isDisplayPressKey;
		}
		
	}


	@Override
	public void ProcessKeyboard(GamePlay gamePlay) {
		// TODO Auto-generated method stub
	}
}
